let logger = require('pomelo-logger').getLogger(__filename);
let pomelo = require("pomelo");

module.exports = function () {
    return new Filter();
};

let Filter = function () {

};

Filter.prototype.before = function (msg, session, next) {
    let app = pomelo.app;
    let playerService = app.get("playerService");
    let player = playerService.findByConnectionId(session["frontendId"], session["id"]);
    if (player == null && msg.__route__ !== "account.loginHandler.login") {
        logger.error("not login player");
        app.get("backendSessionService").kickBySid(session["frontendId"], session["id"]);
        next(null);
        return;
    }
    if (player != null && msg.__route__ === "account.loginHandler.login") {
        logger.error("can not login again");
        app.get("backendSessionService").kickBySid(session["frontendId"], session["id"]);
        next(null);
        return;
    }
    session["player"] = player;
    next(null);
};

Filter.prototype.after = function (err, msg, session, resp, next) {
    next(err);
};
